package leetcode;

/**
 * 完全背包问题：零钱兑换
 */
public class Solution50 {
    public int coinChange(int[] coins, int amount) {
        if(coins == null || coins.length == 0){
            return -1;
        }
        int max = Integer.MAX_VALUE;
        int[] dp = new int[amount + 1];
        //将数组初始化为最大值
        for(int i = 0;i < dp.length;i++){
            dp[i] = max;
        }
        dp[0] = 0;
        for(int i = 0;i < coins.length;i++){  //先物品再背包
            for(int j = coins[i] ;j <= amount;j++){
                if(dp[j - coins[i]] != max){
                    dp[j] = Math.min(dp[j],dp[j - coins[i]] + 1);
                }
            }
        }
        return dp[amount] == max ? -1 : dp[amount];
    }
}
